#include "comm.hpp"

extern Log log;

int main()
{
    Init init;//创建管道
    int shmid=CreateShm();
    char*shmaddr=(char*)shmat(shmid,nullptr,0);

    //IPC code
    int fd=open(FIFO_FILE,O_RDONLY);
    if(fd<0)
    {
       log(Fatal, "error string: %s, error code: %d", strerror(errno), errno);
       exit(FIFO_OPEN_ERR);
    }

    struct shmid_ds shmds;//管理共享内存信息的表
    while(true)
    {
        char c;
        ssize_t s=read(fd,&c,1);//从管道中读到字节流才能访问共享内存
        if(s<=0) break;
        cout<<"clinet say@"<<shmaddr<<endl;
        sleep(1);//避免多进程中读取信息的混乱

        shmctl(shmid,IPC_STAT,&shmds);//控制行为，将shmid_ds中的值设置为当前共享内存关联值
        cout<<"shm size: "<<shmds.shm_segsz<<endl;
        cout<<"shm nattch: "<<shmds.shm_nattch<<endl;
        printf("shm key: 0x%x\n",shmds.shm_perm.__key);
        cout<<"shm mode: "<<shmds.shm_perm.mode<<endl;
    }
    shmdt(shmaddr);//与共享内存去关联
    shmctl(shmid,IPC_RMID,nullptr);//删除共享内存
    close(fd);
    return 0;
}